package org.activiti.engine.impl.bpmn.parser.handler;

import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.impl.Condition;
import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.el.GroovyCondition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ScopeImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.apache.commons.lang3.StringUtils;

public class SequenceFlowParseHandler extends AbstractBpmnParseHandler<SequenceFlow> {
   public static final String PROPERTYNAME_CONDITION = "condition";
   public static final String PROPERTYNAME_CONDITION_TEXT = "conditionText";

   @Override
   public Class<? extends BaseElement> getHandledType() {
      return SequenceFlow.class;
   }



   @Override
   protected void executeParse(BpmnParse bpmnParse, SequenceFlow sequenceFlow) {
      ScopeImpl scope = bpmnParse.getCurrentScope();
      ActivityImpl sourceActivity = scope.findActivity(sequenceFlow.getSourceRef());
      ActivityImpl destinationActivity = scope.findActivity(sequenceFlow.getTargetRef());
      Expression skipExpression;
      if (StringUtils.isNotEmpty(sequenceFlow.getSkipExpression())) {
         ExpressionManager expressionManager = bpmnParse.getExpressionManager();
         skipExpression = expressionManager.createExpression(sequenceFlow.getSkipExpression());
      } else {
         skipExpression = null;
      }

      TransitionImpl transition = sourceActivity.createOutgoingTransition(sequenceFlow.getId(), skipExpression);
      bpmnParse.getSequenceFlows().put(sequenceFlow.getId(), transition);
      transition.setProperty("name", sequenceFlow.getName());
      transition.setProperty("documentation", sequenceFlow.getDocumentation());
      transition.setDestination(destinationActivity);
      if (StringUtils.isNotEmpty(sequenceFlow.getConditionExpression())) {
         Condition expressionCondition = new GroovyCondition(sequenceFlow.getConditionExpression());
         transition.setProperty("conditionText", sequenceFlow.getConditionExpression());
         transition.setProperty("condition", expressionCondition);
      }

      this.createExecutionListenersOnTransition(bpmnParse, sequenceFlow.getExecutionListeners(), transition);
   }

}
